package com.tdam2011.grupo05.database.dao;

import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.google.common.collect.Lists;
import com.tdam2011.grupo05.database.content.TdamContentProvider;
import com.tdam2011.grupo05.entities.contact.ContactWeb;

public class ContactWebDao implements Dao<ContactWeb> {

	private static final String[] PROJECTION = { ContactWeb.COL_ID,
			ContactWeb.COL_ID_PHONE, ContactWeb.COL_NAME,
			ContactWeb.COL_ID_CONTACTO };

	private final ContentResolver cr;

	public ContactWebDao(Context context) {
		cr = context.getContentResolver();
	}

	@Override
	public long save(ContactWeb t) {
		final ContentValues cv = new ContentValues();
		cv.put(ContactWeb.COL_ID_PHONE, t.getIdPhone());
		cv.put(ContactWeb.COL_ID_CONTACTO, t.getIdContacto());
		cv.put(ContactWeb.COL_NAME, t.getName());
		Uri u = cr.insert(TdamContentProvider.USER_CONTENT_URI, cv);
		return ContentUris.parseId(u);
	}

	@Override
	public void update(ContactWeb t) {
		final ContentValues cv = new ContentValues();
		cv.put(ContactWeb.COL_ID, t.getId());
		cv.put(ContactWeb.COL_ID_PHONE, t.getIdPhone());
		cv.put(ContactWeb.COL_ID_CONTACTO, t.getIdContacto());
		cv.put(ContactWeb.COL_NAME, t.getName());
		cr.update(TdamContentProvider.USER_CONTENT_URI, cv, BaseColumns._ID
				+ "=?", new String[] { String.valueOf(t.getId()) });
	}

	public ContactWeb getContactWebByName(String name) {
		ContactWeb c = null;
		Cursor cursor = getCursor(ContactWeb.COL_NAME + "=?",
				new String[] { String.valueOf(name) }, "1");
		if (cursor.moveToFirst()) {
			c = buildContactWeb(cursor);
		}
		if (!cursor.isClosed()) {
			cursor.close();
		}
		return c;
	}

	@Override
	public void delete(ContactWeb t) {
		cr.delete(TdamContentProvider.USER_CONTENT_URI, BaseColumns._ID + "=?",
				new String[] { String.valueOf(t.getId()) });
	}

	@Override
	public ContactWeb get(long id) {
		ContactWeb c = null;
		Cursor cursor = getCursor(BaseColumns._ID + "=?",
				new String[] { String.valueOf(id) }, "1");
		if (cursor.moveToFirst()) {
			c = buildContactWeb(cursor);
		}
		if (!cursor.isClosed()) {
			cursor.close();
		}
		return c;
	}

	@Override
	public List<ContactWeb> getAll() {
		List<ContactWeb> mList = Lists.newArrayList();
		Cursor c = getCursor(null, null, "1");
		if (c.moveToFirst()) {
			do {
				mList.add(buildContactWeb(c));
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return mList;
	}

	@Override
	public Cursor getCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return cr.query(TdamContentProvider.USER_CONTENT_URI, PROJECTION,
				selection, selectionArgs, sortOrder);
	}

	private ContactWeb buildContactWeb(Cursor c) {
		if (c != null) {
			final ContactWeb contact = new ContactWeb();
			contact.setId(c.getLong(c.getColumnIndex(ContactWeb.COL_ID)));
			contact.setIdPhone(c.getInt(c.getColumnIndex(ContactWeb.COL_ID_PHONE)));
			contact.setIdContacto(c.getInt(c
					.getColumnIndex(ContactWeb.COL_ID_CONTACTO)));
			contact.setName(c.getString(c.getColumnIndex(ContactWeb.COL_NAME)));
			return contact;
		} else {
			return null;
		}
	}

}
